package cn.source.system.utils;

import cn.source.common.utils.spring.SpringUtils;
import cn.source.system.domain.ConnInfo;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConnUtils {

    // 定义 ANSI 颜色代码
    final static String ANSI_RESET = "\u001B[0m";
    final static String ANSI_RED = "\u001B[31m";
    final static String ANSI_GREEN = "\u001B[32m";
    final static String ANSI_YELLOW = "\u001B[33m";
    static final String ANSI_BLUE = "\u001B[34m";
    static final String ANSI_PURPLE = "\u001B[35m";
    static final String ANSI_WHITE = "\u001B[37m";


    /**
     * 查询数据库连接
     *
     * @param connName
     * @param sql
     * @return
     */
    public static List<Map<String, Object>> getConnQueryResult(String connName, String sql) {
        JdbcTemplate jdbcTemplate = SpringUtils.getBean(connName);
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }

    public static void execOutFile(String connName, String sql) {
        JdbcTemplate jdbcTemplate = SpringUtils.getBean(connName);
        try {
//            打印绿色文本
//            System.out.println(ANSI_GREEN + sql + ANSI_RESET);
            jdbcTemplate.execute(sql);
        } catch (Exception e) {
            if (e.getCause() instanceof SQLException) {
                SQLException sqlException = (SQLException) e.getCause();
                System.err.println("SQL 错误代码: " + sqlException.getErrorCode());
                System.err.println("SQL 错误消息: " + sqlException.getMessage());
            }
            System.err.println("执行 SQL 语句时发生错误: " + e.getMessage());
            throw e;
        }
    }

    public static int updateOutFile(String connName, String sql) {
        JdbcTemplate jdbcTemplate = SpringUtils.getBean(connName);
        try {
            //            打印绿色文本
//            System.out.println(ANSI_GREEN + sql + ANSI_RESET);
            return jdbcTemplate.update(sql);
        } catch (Exception e) {
            if (e.getCause() instanceof SQLException) {
                SQLException sqlException = (SQLException) e.getCause();
                System.err.println("SQL 错误代码: " + sqlException.getErrorCode());
                System.err.println("SQL 错误消息: " + sqlException.getMessage());
            }
            System.err.println("执行 SQL 语句时发生错误: " + e.getMessage());
            throw e;
        }
    }


    public static ConnInfo urlParse(String url) {
        //解析出host 数据库 端口
        // 定义正则表达式来匹配 JDBC URL
        String regex = "jdbc:mysql://([^:]+):(\\d+)/([^?]+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);

        if (matcher.find()) {
            String host = matcher.group(1);
            String port = matcher.group(2);
            String database = matcher.group(3);

            ConnInfo info = new ConnInfo(host, port, database);
            return info;
        }
        return null;
    }


}
